// pages/join/join.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    compType: [],
    compIdx: 0,
    agree: false,
    vip: true,
    hasCard: true,

  },

  selectTap: function(e) { //选择领域
    console.log(e)
    let that = this;
    let idx = e.detail.value;
    that.setData({
      compIdx: idx
    })
  },

  agreeTap: function() { //同意协议
    let that = this;
    that.data.agree = !that.data.agree;
    that.setData({
      agree: that.data.agree
    })
  },


  submitTap: function(e) { //提交
    console.log(e)
    let that = this;
    let { add_product_img, agree, compType, compIdx } = that.data;
    let formId = e.detail.formId;
    let params = e.detail.value;
    params.com_area = compType[compIdx].id
    if (params.name.length == 0) {
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none'
      })
      return;
      
    } else if (params.phone.length == 0) {
      wx.showToast({
        title: '请输入联系方式！',
        icon: 'none'
      })
      return;
    } else if (params.address.length == 0) {
      wx.showToast({
        title: '请输入详细地址！',
        icon: 'none'
      })
      return;
    } else if (params.company.length == 0) {
      wx.showToast({
        title: '请输入公司名称！',
        icon: 'none'
      })
      return;
    } else if (params.job.length == 0) {
      wx.showToast({
        title: '请输入公司职务！',
        icon: 'none'
      })
      return;
    } else if (params.compType.length == 0) {
      wx.showToast({
        title: '请选择公司所属领域',
        icon: 'none'
      })
      return;
    } else if (!agree) {
      wx.showToast({
        title: '请阅读并同意协议！',
        icon: 'none'
      })
      wx.pageScrollTo({
        selector: '#agree',
        duration: 300
      })
      return;
    } else { //购买
      console.log("提交")
      params = JSON.stringify(params)
      wx.navigateTo({
        url: '/pages/pay/pay?params=' + params,
      })
    }

  },

  openTips(e){ //入会条款,隐私政策
    let tip = e.currentTarget.dataset.tip;
    console.log(tip)
    wx.navigateTo({
      url: '/pages/rule/rule?tip=' + tip,
    })
  },
  openImages(e) { //入会条款,隐私政策
    wx.navigateTo({
      url: '/pages/images/images',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.request({
      url: app.globalData.host + 'member/getUserInfo',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { user_id:wx.getStorageSync('userId') },
      method: 'GET',
      success: function (res) {
        console.log(res)
        if (res.data.data.user.is_card != 1) {
          that.setData({
            hasCard: false,
          })
          return;
        }
        if (res.data.data.user.enabled != 1){
          that.setData({
            vip: false,
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })

    that.getJobType();
  },


  getJobType: function () { //搜索行业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'company/getCompanyArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        wx.setStorageSync('AssocType', res.data.data)
        that.setData({
          compType: res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  createCardTap: function (e) { //创建名片
    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/card-msg-add/card-msg-add',
        })
      })
  },
  checkCardTap: function (e) {  //去认证名片
    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/card-check/card-check',
        })
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var user_id = wx.getStorageSync('userId')
    app.request(app.globalData.host + 'index/checkvip', {
      user_id
    }, 'GET', false,
      function (res) {
        console.log(res)
        if (res.data.code == 1) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel:false,
            complete:function(){
              wx.navigateBack({
                delta : 1
              })
            }
          })
        }
      },
      function (err) { },
      function (complete) { }
    )
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  touchmoveTap(){},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


})